import 'package:flutter/widgets.dart';

import 'hud_content.dart';

export 'hud_content.dart';

class ProgressHUD {
  static HUDController showToastHUD(
    String text, {
    HUDControllerConfigBlock? configBlock,
    double duration = HUDStyle.$defaultDurationTag,
    Alignment? alignment,
  }) {
    return _showHUD(HUDMode.toast,
        configBlock: configBlock,
        alignment: alignment ?? HUDStyle.defaultToastAlignment,
        text: text,
        duration: duration);
  }

  static HUDController showInfoHUD(
    String text, {
    HUDControllerConfigBlock? configBlock,
    double duration = HUDStyle.$defaultDurationTag,
    Alignment? alignment,
  }) {
    return _showHUD(HUDMode.info,
        configBlock: configBlock,
        text: text,
        alignment: alignment ?? HUDStyle.defaultHUDAlignment,
        duration: duration);
  }

  static HUDController showResultHUD(bool success, String text,
      {HUDControllerConfigBlock? configBlock,
      double duration = HUDStyle.$defaultDurationTag,
      Alignment? alignment}) {
    if (success) {
      return showSuccessHUD(text,
          configBlock: configBlock, duration: duration, alignment: alignment);
    } else {
      return showErrorHUD(text,
          configBlock: configBlock, duration: duration, alignment: alignment);
    }
  }

  static HUDController showSuccessHUD(
    String text, {
    HUDControllerConfigBlock? configBlock,
    double duration = HUDStyle.$defaultDurationTag,
    Alignment? alignment,
  }) {
    return _showHUD(HUDMode.success,
        configBlock: configBlock,
        text: text,
        alignment: alignment ?? HUDStyle.defaultHUDAlignment,
        duration: duration);
  }

  static HUDController showWarningHUD(
    String text, {
    HUDControllerConfigBlock? configBlock,
    double duration = HUDStyle.$defaultDurationTag,
    Alignment? alignment,
  }) {
    return _showHUD(HUDMode.warning,
        configBlock: configBlock,
        text: text,
        alignment: alignment ?? HUDStyle.defaultHUDAlignment,
        duration: duration);
  }

  static HUDController showErrorHUD(
    String text, {
    HUDControllerConfigBlock? configBlock,
    double duration = HUDStyle.$defaultDurationTag,
    Alignment? alignment,
  }) {
    return _showHUD(HUDMode.error,
        configBlock: configBlock,
        text: text,
        alignment: alignment ?? HUDStyle.defaultHUDAlignment,
        duration: duration);
  }

  static HUDController showIndeterminateHUD({
    String? text,
    String? withoutDotText,
    HUDControllerConfigBlock? configBlock,
  }) {
    return _showHUD(
      HUDMode.indeterminate,
      text: text,
      configBlock: (controller) {
        if (withoutDotText != null) {
          controller.withoutDotText = withoutDotText;
          controller.reload();
        }
        controller = configBlock?.call(controller) ?? controller;
        return controller;
      },
    );
  }

  static HUDController showDeterminateHUD({
    String? text,
    String? withoutDotText,
    HUDControllerConfigBlock? configBlock,
  }) {
    return _showHUD(
      HUDMode.determinate,
      text: text,
      configBlock: (controller) {
        if (withoutDotText != null) {
          controller.withoutDotText = withoutDotText;
          controller.reload();
        }
        controller = configBlock?.call(controller) ?? controller;
        return controller;
      },
    );
  }

  static HUDController _showHUD(HUDMode mode,
      {HUDControllerConfigBlock? configBlock,
      String? text,
      bool dimBackground = false,
      Alignment? alignment,
      double? duration}) {
    if (duration == HUDStyle.$defaultDurationTag) {
      duration = HUDStyle.defaultDuration;
    }

    HUDController controller = HUDController(
      mode,
      text: text,
      alignment: alignment ??
          (mode == HUDMode.toast
              ? HUDStyle.defaultToastAlignment
              : HUDStyle.defaultHUDAlignment),
    );

    Duration? hideDelay;

    if (duration != null && duration > 0.0) {
      hideDelay = Duration(milliseconds: (duration * 1000).toInt());
    }
    controller = configBlock?.call(controller) ?? controller;
    controller.showHUD(dimBackground: dimBackground, duration: hideDelay);
    return controller;
  }
}
